//
//  BaseButton.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/21.
//

import UIKit
import RxSwift
import RxCocoa

class BaseButton: UIButton {
    private lazy var disposeBag = DisposeBag()
    var enabledScaleFlag: Bool = true//是否需要放大字体
    override init(frame: CGRect) {
        super.init(frame: frame)
        //监听字体变化
        Constant.standardViewFlagSuject.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.enabledScaleFlag {
                strongSelf.setRealFontSize()
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 正常设置font
    func setFont(_ font: UIFont?) {
        if let font = font {
            if Constant.standardViewFlag {
                self.titleLabel?.font = font
            }else{
                self.titleLabel?.font = font.withSize(font.pointSize * Constant.magnifiedViewScale)
            }
            
        }
    }
    func disableScaleFlag(){
        self.enabledScaleFlag = false
    }
    //MARK: - 设置真正的size
    private func setRealFontSize(){
        if let font = self.titleLabel?.font {
            self.titleLabel?.font = font.withSize(font.pointSize * Constant.currentSizeScale)
        }
        
    }
    //单独放大字体
    private func setLargeFontSize(){
        if let font = self.titleLabel?.font {
            self.titleLabel?.font = font.withSize(font.pointSize * Constant.magnifiedViewScale)
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
